<?php
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/4/21 10:05
 * Desc:
 */

namespace App\Service;


use App\Lib\Common;
use App\Models\Admin\AdminUser;
use App\Models\Admin\Org;
use App\Models\Admin\Site;
use Illuminate\Http\Request;

class AdminUserService extends BaseService
{
    protected $modelName="Admin.AdminUser";
    /**
     * 修改登录信息
     * @params $type 1表示登录成功，2表示登录出错
     */
    public function setLogin(Request $request, $type, $name = "", $guard_name) {
        switch ($type) {
            case 1:
                \auth($guard_name)->user()->session_id = "" . \auth($guard_name)->getSession()->getId() . "";
                \auth($guard_name)->user()->login_ip = $request->getClientIp();
                \auth($guard_name)->user()->login_time = time();
                \auth($guard_name)->user()->password_error_times = 0;
                \auth($guard_name)->user()->password_last_fail_time = 0;
                \auth($guard_name)->user()->save();
                session(['guard' => $guard_name]);
                break;
            case 2:
                //自增错误次数
                $user = (new AdminUser())->where("name", $name)->first();
                $user->increment("password_error_times");
                $user->password_last_fail_time = time();
                $user->save();
                break;
        }
    }

    //密码错误次数
    public function getErrorTime($name) {
        $user = (new AdminUser())->where("name", $name)->first()->toArray();
        return ["num"=>$user["password_error_times"],"site_id"=>$user["site_id"]];
    }

    //获取用户列表
    public function list($size, $params = []) {
        $like=[];
        if (array_key_exists("name", $params) && !empty($params["name"]) && "" != $params["name"] && null != $params["name"]) {
            $like[] = ["name","like","%" . $params["name"] . "%"];
        }
        if (array_key_exists("real_name", $params) && !empty($params["real_name"]) && "" != $params["real_name"] && null != $params["real_name"]) {
            $like[] = ["real_name","like","%" . $params["real_name"] . "%"];
        }
        if(array_key_exists("org_id", $params) && !empty($params["org_id"]) && "" != $params["org_id"] && null != $params["org_id"] ){
            if($params["org_id"]==getCurrentSiteId()){
                unset($params["org_id"]);
            }else{
                $org = (new OrgService())->first(["id"=>$params["org_id"]]);
                $like[] = ['org_code', 'like', $org["code"] . "%"];
            }

        }
        $result = (new AdminUser())->getAll($size, $params,$like);
        return $result;
    }

    //注册/添加用户
    public function register($params) {
        //数据处理
        $params['salt'] = (new Common())->get_salt();
        $params["password"] = (new Common())->passWord($params['salt'], $params["password"]);
        if(null==$params['org_id']||empty($params['org_id'])||""==$params["org_id"]){$params["org_id"]=0;}
        //查找组织机构代码
        if ($params["org_id"] == 0) {
            $params["org_code"] = "";
        } else {
            $org = (new Org())->find($params["org_id"])->toArray();
            $params["org_code"] = $org["code"];
        }
        $params["site_id"] = $params['creator'] = getCurrentSiteId();
        //插入
        $result = (new AdminUser())->create($params);
        return $result;
    }

    //获取全部站点
    public function selectSite() {
        return (new Site())->get(["id", "name"])->toArray();
    }

    //新用户登录时是否修改密码
    public function isInit($params) {
        return (new AdminUser())->initOrNo($params);
    }

    //用户登录初始化修改密码
    public function changePwd($id, $password) {
        return (new AdminUser())->changePwd($id, $password);
    }

    //管理员/用户自身修改密码
    public function pwdEdit($id, $params) {
        $params["pwd_init"] = array_key_exists("pwd_init", $params) && $params["pwd_init"] == "on" ? 1 : 0;
        return (new AdminUser())->changeUserPwd($id, $params);
    }

    /**
     * 更新账号信息
     */
    public function updateInfo($params, $id) {
        //处理头像
        if ($params["avatar"] == null || $params["avatar"] == "" || empty($params["avatar"])) {
            unset($params["avatar"]);
        }
        //处理数据
        $params["is_locked"] = array_key_exists("is_locked", $params) && $params["is_locked"] == "on" ? 0 : 1;
        if(null==$params['org_id']||empty($params['org_id'])||""==$params["org_id"]){$params["org_id"]=0;}
        //查找组织机构代码
        if ($params["org_id"] == 0) {
            $params["org_code"] = "";
        } else {
            $org = (new Org())->find($params["org_id"])->toArray();
            $params["org_code"] = $org["code"];
        }
        unset($params["_token"]);
        unset($params["password_confirmation"]);
        unset($params["imgup"]);
        unset($params["open"]);
        unset($params["select"]);
        //更新
        return (new AdminUser())->renew($params, $id);
    }
}
